package io.xccit.aischool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xccit.aischool.entity.Grade;
import io.xccit.aischool.mapper.IGradeMapper;
import io.xccit.aischool.service.IGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author CH_ywx
 * @date 2023-06-06
 * @description
 */
@Service("gradeService")
@Transactional
public class GradeServiceImpl extends ServiceImpl<IGradeMapper, Grade> implements IGradeService {

    @Autowired
    private IGradeMapper gradeMapper;

    /**
     * 带分页查询条件的年级查询(与模糊查询公用一个方法,有gradeName就是模糊查询)
     * @param page 分页
     * @param gradeName 年级名称(模糊查询字段)
     * @return
     */
    @Override
    public IPage getGradesByOpr(Page page, String gradeName) {
        QueryWrapper<Grade> queryWrapper = new QueryWrapper<>();
        //如果gradeName不为空,模糊查询结果
        if (!StringUtils.isEmpty(gradeName)) {
            queryWrapper.like("name",gradeName);
        }
        return gradeMapper.selectPage(page,queryWrapper);
    }

    /**
     * 查询所有年级
     * @return 年级
     */
    @Override
    public List<Grade> getAllGrades() {
        QueryWrapper<Grade> queryWrapper = new QueryWrapper<>();
        return gradeMapper.selectList(queryWrapper);
    }
}
